#define  _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"
void SLInit(SL* ps1)
{
	ps1->a = (SLDatatype*)malloc(sizeof(SLDatatype) * 4);
	if (ps1->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps1->size = 0;
	ps1->capacity = 4;
}
void SLDestory(SL* ps1)
{
	free(ps1->a);
	ps1->a = NULL;
	ps1->size = 0;
}
void SLPrint(SL* ps1)
{
	for (int i = 0; i < ps1->size; i++)
	{
		printf("%d", ps1->a[i]);
	}
	printf("\n");
}
void SLCheckCapacity(SL* ps1)
{
	if (ps1->size == ps1->capacity)
	{
		SLDatatype* tmp =(SLDatatype*)
			realloc(ps1->a, sizeof(SLDatatype) * ps1->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps1->a = tmp;
		ps1->capacity *= 2;
	}
}


void SLpushBack(SL* ps1, SLDatatype x)
{
	SLCheckCapacity(ps1);
	ps1->a[ps1->size++] = x;
}

void SLpushFront(SL* ps1, SLDatatype x)
{
	SLCheckCapacity(ps1);
	for (int i = ps1->size-1; i >= 0; i--)
	{
		ps1->a[i + 1] = ps1->a[i];
	}
	ps1->a[0] = x;
	ps1->size++;
}

void SLInsert(SL* ps1, int pos, SLDatatype x)
{
	assert(pos >= 0 && pos <= ps1->size);
	SLCheckCapacity(ps1);
	for (int i = ps1->size - 1; i >= pos; i--)
	{
		ps1->a[i+1] = ps1->a[i];
	}

	ps1->a[pos] = x;
	ps1->size++;
   
}



void SLErase(SL* ps1, int pos)
{
	assert(pos >= 0 && pos <= ps1->size);
	SLCheckCapacity(ps1);
	for (int i = pos+1; i <= ps1->size-1; i++)
	{
		ps1->a[i -1] = ps1->a[i];
	}
	ps1->size--;

}
int SLFind(SL* ps1, SLDatatype x)
{

	for (int i = 0; i < ps1->size; i++)
	{
		if (ps1->a[i] == x)
		{
			return i;
		}

	}
	return -1;
}

void SLModify(SL* ps1, int pos, SLDatatype x)
{
	assert(pos >= 0 && pos <= ps1->size);
	ps1->a[pos] = x;
}

